package com.gec.teams.wechat.common;

import com.gec.teams.wechat.exception.TeamsException;
import com.gec.teams.wechat.properties.TxOssProperties;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.UUID;

/**
 * 功能：
 * 作者：YumTea
 * 日期：2024/6/20 10:36
 */
@Component
@Data
@Slf4j
public class UpLoadFileComponent {
    @Autowired
    private TxOssProperties txOssProperties;

    public String uploadFile(MultipartFile file){
        //TODO 再分开一个文件夹，分开上班和下班
        //获取原来的名称
        String oldFileName = file.getOriginalFilename();
        String eName = oldFileName.substring(oldFileName.lastIndexOf("."));
        //生成新名字
        String newFileName = UUID.randomUUID()+eName;
        //已经获取当前时间，其实用date我感觉也可以
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1;
        int day = cal.get(Calendar.DATE);
        //1、初始化身份信息
        BasicCOSCredentials card = new BasicCOSCredentials(txOssProperties.getAccessKey(), txOssProperties.getSecretKey());
        //2、设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/622
        ClientConfig config = new ClientConfig(new Region(txOssProperties.getBucket()));
        //3、生成cos客户端
        COSClient cosClient = new COSClient(card, config);
        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
        // 大文件上传请参照 API 文档高级 API 上传
        File localFile = null;
        //开始上传图片
        try{
            localFile= File.createTempFile("temp",null);
            file.transferTo(localFile);
            //指定要上传的路径,就会生成图片链接
            String key = "/"+txOssProperties.getQianzui()+"/"+year+"/"+month+"/"+day+"/"+newFileName;
            //上传
            PutObjectRequest putObjectRequest = new PutObjectRequest(txOssProperties.getBucketName(), key, localFile);
            //接收返回的数据
            PutObjectResult result = cosClient.putObject(putObjectRequest);
            log.info("上传结果:{}",result);
            return "上传成功"+txOssProperties.getPath() + putObjectRequest.getKey();
//            return new UploadMsg(1,"上传成功",this.path + putObjectRequest.getKey());
        } catch (IOException e) {
            throw new TeamsException("上传图片失败",200,e);
        }finally {
            //关闭客户端
            cosClient.shutdown();
        }
    }
    public String uploadFile(MultipartFile file,Integer status){
        //TODO 再分开一个文件夹，分开上班和下班
        //获取原来的名称
        String oldFileName = file.getOriginalFilename();
        String eName = oldFileName.substring(oldFileName.lastIndexOf("."));
        //生成新名字
        String newFileName = UUID.randomUUID()+eName;
        //已经获取当前时间，其实用date我感觉也可以
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1;
        int day = cal.get(Calendar.DATE);
        //1、初始化身份信息
        BasicCOSCredentials card = new BasicCOSCredentials(txOssProperties.getAccessKey(), txOssProperties.getSecretKey());
        //2、设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/622
        ClientConfig config = new ClientConfig(new Region(txOssProperties.getBucket()));
        //3、生成cos客户端
        COSClient cosClient = new COSClient(card, config);
        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
        // 大文件上传请参照 API 文档高级 API 上传
        File localFile = null;
        //开始上传图片
        try{
            localFile= File.createTempFile("temp",null);
            file.transferTo(localFile);
            String workOrDwon= status<=2?"work":"donw";

            //指定要上传的路径,就会生成图片链接 一个/代表一个文件夹
            String key = "/"+txOssProperties.getQianzui()+"/"+year+"/"+month+"/"+day+"/"+workOrDwon+"/"+newFileName;
            //上传
            PutObjectRequest putObjectRequest = new PutObjectRequest(txOssProperties.getBucketName(), key, localFile);
            //接收返回的数据
            PutObjectResult result = cosClient.putObject(putObjectRequest);
            return txOssProperties.getPath() + putObjectRequest.getKey();
        } catch (IOException e) {
            throw new TeamsException("上传图片失败",200,e);
        }finally {
            //关闭客户端
            cosClient.shutdown();
        }
    }
}
